package me.young4979.android.virtualapkdemo;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.didi.virtualapk.PluginManager;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
        findViewById(R.id.init).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String pluginPath = Environment.getExternalStorageDirectory().getAbsolutePath
                            ().concat("/pluginApp1.apk");
                    File plugin = new File(pluginPath);
                    PluginManager.getInstance(MainActivity.this).loadPlugin(plugin);
                    pluginPath = Environment.getExternalStorageDirectory().getAbsolutePath
                            ().concat("/wxPluginApp.apk");
                    plugin = new File(pluginPath);
                    PluginManager.getInstance(MainActivity.this).loadPlugin(plugin);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClassName("me.young4979.android.pluginapp1", "me.young4979.android" +
                        ".pluginapp1" +
                        ".Plugin1Activity");
                startActivity(intent);
            }
        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClassName("me.young4979.android.pluginapp1", "me.young4979.android" +
                        ".pluginapp1" +
                        ".Plugin1Activity2");
                intent.putExtra("data", "来自宿主工程的数据");
                startActivity(intent);
            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClassName("me.young4979.android.pluginapp1", "me.young4979.android" +
                        ".library1" +
                        ".Lib1Activity");
                startActivity(intent);
            }
        });
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClassName("me.young4979.android.wxpluginapp", "me.young4979.android" +
                        ".wxpluginapp" +
                        ".WeexActivity");
                startActivity(intent);
            }
        });
    }

    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean enableReadStorage = ContextCompat.checkSelfPermission(this, Manifest
                    .permission.READ_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED;
            boolean enableWriteStorage = ContextCompat.checkSelfPermission(this, Manifest
                    .permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED;
            if (!enableReadStorage || !enableWriteStorage) {
                ActivityCompat.requestPermissions(this, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission
                        .WRITE_EXTERNAL_STORAGE}, 3);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 3) {
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    // 未授权
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                            .setMessage("如果禁止该权限，您将无法登录")
                            .setNegativeButton("退出", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    finish();
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    finish();
                                    checkPermission();
                                }
                            });
                    builder.show();
                    //Toast.makeText(this, "未授权", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
